package com.jdbc;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;



public class MainJDBC {

    /**
     * JDBC
     * @param args
     */
    public static void main(String[] args) {

        try {

//    1、加载驱动
            Class.forName("com.mysql.jdbc.Driver");

//    2、创建连接
            Connection connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3308/01spring",
                    "root",
                    "123456");
            System.out.println(connection);
//    3、定义sql语句：select * from account;
            String sql="select * from account";

//       sql注入问题
//       int id=11;
////         String  id="999999 or 1=1";
//       int balance=100;
//       String sql="SELECT * FROM `account` where id= "+id+" and balance="+balance+"";

//    4、执行sql语句，获取结果集resultSet
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);

//    5、遍历查询的结果
            while(resultSet.next()) {
                System.out.println("第1字段-->"+resultSet.getInt(1));
                System.out.println("第2字段-->"+resultSet.getString(2));
                System.out.println("第3字段-->"+resultSet.getDouble(3));
            }


//    6、关闭连接
            connection.close();
            statement.close();
            resultSet.close();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }


}